/**   
 * Copyright © 2016 公司名. All rights reserved.
 * 
 * @Title: PrivateKeyServiceImpl.java 
 * @Prject: LianniuWeb
 * @Package: com.lianniu.web.privateKey.service.impl
 * @Description: TODO
 * @author: Administrator   
 * @date: 2016年11月14日 上午9:25:17 
 * @version: V1.0   
 */
package com.lianniu.web.privateKey.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lianniu.web.privateKey.dao.PrivateKeyDao;
import com.lianniu.web.privateKey.model.PrivateKeyVo;
import com.lianniu.web.privateKey.service.impl.PrivateKeyServiceImpl;
import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.privateKey.service.IPrivateKeyService;

/** 
 * @ClassName: PrivateKeyServiceImpl 
 * @Description: 验证邀请码
 * @author: Administrator
 * @date: 2016年11月14日 上午9:25:17  
 */
@Service("privateKeyService")
public class PrivateKeyServiceImpl implements IPrivateKeyService {
    
    private final static Logger logger = Logger.getLogger(PrivateKeyServiceImpl.class);
    @Autowired
    private PrivateKeyDao privateKeyDao;

   /** 
     * @Title: checkPrivateKey
     * @Description: 买家切换到卖家时，验证邀请码
     * @author: lzz  
     * @param userId
     * @param staffId
     * @param staffName
     * @param staffMobile
     * @return BaseResult
     * @see com.lianniu.web.privateKey.service.IPrivateKeyService#checkPrivateKey( java.lang.String)
     * @date: 2016年11月14日 上午9:25:17 
     */
    @Override
    @Transactional
    public BaseResult checkPrivateKey(String supplyId,String card) {
        logger.info("checkPrivateKey begin");
        logger.info("checkPrivateKey param supplyId:["+supplyId+"],card:["+card+"]");
        PrivateKeyVo privateKey = null;
        try {
            logger.info("checkPrivateKey getKeyByCard begin");
            privateKey = privateKeyDao.getKeyByCard(card.toUpperCase());
            logger.info("checkPrivateKey getKeyByCard result privateKey:"+JSON.toJSONString(privateKey));
            logger.info("checkPrivateKey getKeyByCard end");
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("checkPrivateKey getKeyByCard Exception:"+"["+e.getMessage()+"]");
            logger.info("checkPrivateKey getKeyByCard end");
            logger.info("checkPrivateKey end");
            return BaseResult.exception(e.getMessage());
        }
        
        if(null != privateKey){
            
            if(supplyId.equals(privateKey.getSupplyId())){
                logger.info("checkPrivateKey end");
                return BaseResult.success();
            }
            try {
                logger.info("checkPrivateKey updateKeyStatus begin");
                privateKey.setSupplyId(supplyId);
                privateKey.setStatus(2);
                privateKeyDao.updateKeyStatus(privateKey);
                return BaseResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("checkPrivateKey updateKeyStatus Exception:"+"["+e.getMessage()+"]");
                logger.info("checkPrivateKey updateKeyStatus end");
                logger.info("checkPrivateKey end");
                return BaseResult.exception(e.getMessage());
            }
            
        }else{
            logger.info("checkPrivateKey end");
            return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, "邀请码无效!");
        }
        
    }
    /**
     * @Title: checkSupplyKey
     * @Description: 买家切换到卖家时，验证用户是否输入过邀请码
     * @author: lzz  
     * @param userId
     * @see com.lianniu.web.privateKey.service.IPrivateKeyService#checkSupplyKey( java.lang.String)
     * @date: 2016年11月14日 上午9:25:17 
     */
@Override
public BaseResult checkSupplyKey(String userId) {
    logger.info("checkSupplyKey begin");
    try {
        Integer i = privateKeyDao.checkSupplyKey(userId);
        if(null != i && i >0){
            logger.info("checkSupplyKey checkSupplyKey end");
            logger.info("checkSupplyKey end");
            return BaseResult.success();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.info("checkSupplyKey checkSupplyKey Exception:"+"["+e.getMessage()+"]");
        logger.info("checkSupplyKey checkSupplyKey end");
        logger.info("checkSupplyKey end");
        return BaseResult.exception(e.getMessage());
    }
    
    logger.info("checkSupplyKey end");
    return BaseResult.fail("NO0000", "还没输过邀请码");
}

}
